<template>
  <div class="center">
    <!-- 头部 -->
    <div class="table_header flex flex_bet flex-wrap">
      <div class="table_button">
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="Refresh"></el-button>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="formVisible = true">添加</el-button>
      </div>
      <div class="table_searchs flex felx_align">
        <div class="table_search flex">
          <el-input placeholder="搜索" v-model="searchTable" class="input-with-select" size="small">
            <el-select v-model="select" slot="prepend" placeholder="请选择" class="table_select" style="width:105px">
              <el-option label="内容名称" value="1"></el-option>
              <el-option label="分类" value="2"></el-option>
              <el-option label="标签" value="3"></el-option>
              <el-option label="合集" value="4"></el-option>
              <el-option label="批次" value="5"></el-option>
            </el-select>
            <el-button slot="append" icon="el-icon-search" @click="idnameSearch"></el-button>
          </el-input>
        </div>
        <dynamicTable :checkboxVal="checkboxVal" @alter="alterCheckbok"/>
      </div>
    </div>
    <el-table stripe style="width: 100%"  border fit highlight-current-row ref="tableDataRef" :data="tableData" :key="key" @sort-change="tableSortCge">
      <el-table-column v-if="colData[0].istrue" min-width="50" align="center" prop="content_id" label="ID"></el-table-column>
      <el-table-column v-if="colData[1].istrue" min-width="70" align="center" prop="admin_name" label="创建者" show-overflow-tooltip  ></el-table-column>
      <el-table-column v-if="colData[2].istrue" min-width="60" align="center" prop="platter" label="套餐归属">
        <template slot-scope="scope">
          <p v-for="(item,index) in scope.row.platter" :key="item.id" style="margin: 0;padding: 0">{{item.name}}</p>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[3].istrue" min-width="60" align="center" prop="content_name" label="内容名称"></el-table-column>
      <el-table-column v-if="colData[4].istrue" min-width="80" align="center" prop="content_type" label="文件性质" sortable="custom">
        <template slot-scope="scope">
          <span v-if="scope.row.content_type==1">视频</span>
          <span v-if="scope.row.content_type==2">音频</span>
          <span v-if="scope.row.content_type==3">文档</span>
          <span v-if="scope.row.content_type==4">图片</span>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[5].istrue" min-width="60" align="center" prop="content_size" label="文件大小">
        <template slot-scope="scope">{{scope.row.content_size| zhDate}}</template>
      </el-table-column>
      <el-table-column v-if="colData[6].istrue"  min-width="100" align="center" prop="batch" label="分发批次">
        <template slot-scope="scope">
          <div v-if="scope.row.batch.length <= 5">
            <p v-for="(item,index) in scope.row.batch" :key="item.id" style="margin: 0;padding: 0">{{item.batch}}</p>
          </div>
          <div v-else>
            <el-popover
              placement="right"
              width="600"
              trigger="click">
              <span v-for="(item,index) in scope.row.batch" :key="item.id" style="margin: 0;padding: 0">{{item.batch}}&nbsp;&nbsp;&nbsp;&nbsp;</span>
              <el-button slot="reference" size="small">点击查看全部</el-button>
            </el-popover>
          </div>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[7].istrue" min-width="80" align="center" prop="childAdmin" label="分发子账号">
        <template slot-scope="scope">
          <p v-for="(item,index) in scope.row.childAdmin" :key="item.id"  style="margin: 0;padding: 0">{{item.username}}</p>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[8].istrue" min-width="80" align="center" prop="contentClass" label="分类" sortable="custom">
        <template slot-scope="scope">
          <p v-for="(item,index) in scope.row.contentClass" :key="item.id"  style="margin: 0;padding: 0">{{item.name}}</p>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[9].istrue" min-width="80" align="center" prop="contentTag" label="标签" sortable="custom">
        <template slot-scope="scope">
          <p v-for="(item,index) in scope.row.contentTag" :key="item.id"  style="margin: 0;padding: 0">{{item.name}}</p>
        </template>
      </el-table-column>

      <el-table-column v-if="colData[10].istrue" min-width="80" align="center" prop="contentCollect" label="合集" sortable="custom">
        <template slot-scope="scope">
          <p v-for="(item,index) in scope.row.contentCollect" :key="item.id"  style="margin: 0;padding: 0">{{item.name}}</p>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[11].istrue" min-width="90" align="center" prop="grades" label="适配学龄段" >
        <template slot-scope="scope">
          <p v-for="(item,index) in scope.row.grades" :key="index"  style="margin: 0;padding: 0">
            <span v-show="item==1">幼儿</span>
            <span v-show="item==2">小学</span>
            <span v-show="item==3">初中</span>
            <span v-show="item==4">高中</span>
          </p>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[12].istrue" min-width="80" align="center" prop="content_explain" label="说明" show-overflow-tooltip></el-table-column>
      <el-table-column v-if="colData[13].istrue" min-width="60" align="center" prop="readTime" label="查阅次数"> </el-table-column>
      <el-table-column v-if="colData[14].istrue" min-width="50" align="center" prop="read_sn_num"  label="查阅设备数">
        <template slot-scope="scope">
          <p style="color: #0079ff" @click="openReadSn(scope.row.content_id)">{{scope.row.read_sn_num}}</p>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[15].istrue" min-width="50" align="center" prop="noReadSn"  label="待查阅设备数">
        <template slot-scope="scope">
          <p style="color: #0079ff" @click="openNoReadSn(scope.row.content_id)">{{scope.row.noReadSn}}</p>
        </template>
      </el-table-column>
      <el-table-column  label="操作" min-width="120" align="center">
        <template slot-scope="scope">
          <el-row>
            <el-button size="mini" type="primary" @click="openEdit(scope.row)">编辑</el-button>
            <el-button size="mini" @click="delContent(scope.row.content_id)">删除</el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="paging">
      <el-pagination :current-page.sync="listQuery.startSize" background @size-change="handleSizeChange"
                     @current-change="handleCurrentChange" :page-sizes="[10, 50, 100, 200]"
                     layout="total, sizes, prev, pager, next, jumper" :total="total" :page-size="listQuery.pageSize">
      </el-pagination>
    </div>

    <el-dialog title="已查阅设备" :visible.sync="readSnVisible">
      <el-table :data="readSnData">
        <el-table-column property="sn" align="center" label="SN" min-width="150"></el-table-column>
        <el-table-column property="batch" align="center" label="批次" ></el-table-column>
        <el-table-column property="classlevel" align="center" label="班级"></el-table-column>
        <el-table-column property="sno" align="center" label="学号" ></el-table-column>
        <el-table-column property="realname" align="center" label="姓名" ></el-table-column>
        <el-table-column property="phone" align="center" label="监护人1"></el-table-column>
        <el-table-column property="phone_two" align="center" label="监护人2"></el-table-column>
        <el-table-column property="readTime" align="center" label="查看次数"></el-table-column>
        <el-table-column property="create_time" align="center" label="最近一次查看时间" min-width="150">
          <template slot-scope="scope">{{scope.row.create_time| newDate}}</template>
        </el-table-column>
      </el-table>
      <div class="paging">
        <el-pagination :current-page.sync="hasReadQuery.startSize" background @size-change="hasReadSizeChange"
                       @current-change="hasReadCurrentChange" :page-sizes="[10, 50, 100, 200]"
                       layout="total, sizes, prev, pager, next, jumper" :total="hasReadTotal" :page-size="hasReadQuery.pageSize">
        </el-pagination>
      </div>
    </el-dialog>
    <el-dialog title="待查阅设备" :visible.sync="noReadSnVisible">
      <el-table :data="noReadSnData">
        <el-table-column property="value" align="center" label="SN" min-width="150"></el-table-column>
        <el-table-column property="batch" align="center" label="批次" ></el-table-column>
        <el-table-column property="classlevel" align="center" label="班级"></el-table-column>
        <el-table-column property="sno" align="center" label="学号" ></el-table-column>
        <el-table-column property="realname" align="center" label="姓名" ></el-table-column>
        <el-table-column property="phone" align="center" label="监护人1"></el-table-column>
        <el-table-column property="phone_two" align="center" label="监护人2"></el-table-column>
      </el-table>
      <div class="paging">
        <el-pagination :current-page.sync="noReadQuery.startSize" background @size-change="noReadSizeChange"
                       @current-change="noReadCurrentChange" :page-sizes="[10, 50, 100, 200]"
                       layout="total, sizes, prev, pager, next, jumper" :total="noReadTotal" :page-size="noReadQuery.pageSize">
        </el-pagination>
      </div>
    </el-dialog>

    <!-- 添加弹窗 -->
    <el-dialog title="上传内容" :visible.sync="formVisible" :close-on-click-modal="false" :close-on-press-escape="false" width="40%" @close="addFormclose">
      <el-form :model="addForm"  ref="ruleForm" :rules="rules" class="diolog_adapt" style="width: 600px" >
        <el-form-item label="上传方式:" label-width="120px" >
          <el-radio-group v-model="addForm.upload_type" @input="setContentOptions">
            <el-radio :label="1">上传新内容</el-radio>
            <el-radio :label="2">选择已上传内容</el-radio>
            <el-radio :label="3">选择厂商上传内容</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="选择内容" label-width="120px" prop="relation_id" v-show="addForm.upload_type != 1">
          <el-select v-model="addForm.relation_id" placeholder="请选择" @change="setAddForm">
            <el-option
              v-for="item in contentOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="文件上传:" label-width="120px" prop="content_url">
          <div class="flex felx_align">
            <el-upload class="upload-demo" action="#" id="file" :auto-upload="false" :show-file-list="true" :on-change="contentUpload" :file-list="addForm.file_list" :on-remove="handleRemove" :limit="1" :on-exceed="handleExceed">
              <el-button size="small" type="primary"><span>文件上传</span></el-button>
            </el-upload>
          </div>
        </el-form-item>
        <el-form-item label="内容名:" label-width="120px" prop="content_name">
          <el-input v-model="addForm.content_name" placeholder="请输入文件名" maxlength="30"></el-input>
        </el-form-item>
        <el-form-item label="文件性质:" label-width="120px" prop="content_type">
          <el-select v-model="addForm.content_type" placeholder="请选择">
            <el-option
              v-for="item in courseTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="分发方式:" label-width="120px" prop="send_type">
          <el-radio-group v-model="addForm.send_type">
            <el-radio :label="1">批次</el-radio>
            <el-radio :label="2">子账号</el-radio>
            <el-radio :label="3">批次和子账号</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="批次:"  label-width="120px" prop="batch" v-show="addForm.send_type==1 || addForm.send_type==3">
          <AiSelectAllEle
            v-model="addForm.batch"
            :options="batchArr"
            :width="150"
            labelName="batch"
            valueName="id"
          />
        </el-form-item>
        <el-form-item label="子账号:" v-show="addForm.send_type==2 || addForm.send_type==3" prop="childAdmin" label-width="120px">
          <el-select v-model="addForm.childAdmin"  multiple placeholder="请选择子账号">
            <el-option
              v-for="item in childrenOption"
              :key="item.id"
              :label="item.username"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类与合集:" label-width="120px">
          <el-tree ref="tree" :data="classCollectData" :props="defaultProps" show-checkbox node-key="id" ></el-tree>
        </el-form-item>
        <el-form-item label="分类:" label-width="120px" prop="classId" v-if="false">
          <el-select v-model="addForm.classId" multiple filterable placeholder="请选择分类">
            <el-option
              v-for="item in contentClass"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标签:" label-width="120px" prop="tagId">
          <el-select v-model="addForm.tagId" multiple filterable placeholder="请选择标签">
            <el-option
              v-for="item in tagArr"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="合集:" label-width="120px" prop="collectId" v-if="false">
          <el-select v-model="addForm.collectId" multiple filterable placeholder="请选择合集">
            <el-option
              v-for="item in contentCollect"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="适配学龄" label-width="120px" prop="suitable_grade" v-permission="['超级管理员']">
          <el-select v-model="addForm.suitable_grade" multiple filterable  placeholder="请选择适配学龄段" class="add_select">
            <el-option
              v-for="item in gradeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="套餐归属" label-width="120px" prop="platterId" v-permission="['超级管理员']">
          <el-select v-model="addForm.platterId" multiple filterable  placeholder="请选择套餐" class="add_select">
            <el-option
              v-for="item in platterArr"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="说明:" label-width="120px" prop="content_explain">
          <el-input v-model="addForm.content_explain" ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="formVisible = false" size="mini" round>取 消</el-button>
        <el-button v-if="editSubjects" type="primary" size="mini" round @click="editContent('ruleForm')">确 定
        </el-button>
        <el-button v-else type="primary" @click="addContent('ruleForm')" size="mini" round>确定</el-button>
      </div>
    </el-dialog>
    <delete-Dialog title="" :visible.sync="deleteDialogs.show" width="25%" top="40vh" @deleteEvents="deleteEvents" :deleteText="deleteDialogs.deleteText" />
  </div>

</template>

<script>
  import objectable from "@/utils/objectable.js";
  import dynamicTable from "@/components/DynamicTable";
  import deleteDialog from "@/components/deleteDialog";
  import tableMini from '@/utils/tableMini.js'
  import permission from '@/directive/permission/index.js' // 权限判断指令
  import AiSelectAllEle from "@/components/SelectAllEle/index";

  import {
    editContentData,
    addContentControl,
    queryContentControl,
    delContentControl,
    getPlatter,
    getContentClass,
    getTag,
    getContentCollect,
    getChildAdmin, setContentOptions, getSnReadContent, getNoReadContent, getClassCollectTree
  } from "@/api/launcherMade";
  import {ossFile, ossImage} from "@/utils/ossUpload";
  import {getBatchTwo} from "@/api/equipment";

  export default {
    directives: { permission },
    mixins: [tableMini],
    components: {dynamicTable, deleteDialog, AiSelectAllEle},
    data() {
      return {
        key: 1,
        colData: [], //动态控制table
        checkboxVal: ["ID", "创建者", "套餐归属", "内容名称", "文件性质", "文件大小", "批次", "子账号", "分类", "标签", "合集", "适配学龄段", "内容说明", "查阅次数", "查阅设备数", "待查阅设备数"],
        searchTable: "",
        select: "", //搜索条件
        tableData: [],
        listQuery: {
          startSize: 1,
          pageSize: 10,
          userId: localStorage.getItem("ID"),
        },
        hasReadQuery: {
          startSize: 1,
          pageSize: 10,
          content_id: undefined
        },
        noReadQuery: {
          startSize: 1,
          pageSize: 10,
          content_id: undefined
        },
        hasReadTotal: 0,
        noReadTotal: 0,
        addForm: {
          content_name: '',
          content_type: '',
          content_size: '',
          content_explain: '',
          content_url: '',
          batch: [],
          classId: [],
          tagId: [],
          collectId: [],
          suitable_grade: [],
          platterId: '',
          send_type: '',
          childAdmin: [],
          upload_type: 1,
          relation_id: '', //upload_type为1时为self_content表content_id 为2时为course表主键id
          file_list: [],
        },
        formVisible: false,
        rules: {
          content_name: [{required: true, message: '请输入内容名称', trigger: 'blur'}],
          content_type: [{ required: true, message: '请选择文件性质', trigger: 'change' }],
          content_url: [{required: true, message: '请上传文件', trigger: 'blur'}],
          send_type: [{ required: true, message: '请选择分发方式', trigger: 'change' }],
          // batch: [{ required: true, message: '请选择批次', trigger: 'change' }],
          // tagId: [{ required: true, message: '请选择标签', trigger: 'change' }],
        },
        editSubjects: false,
        deleteDialogs: { show: false, deleteText: '', deleteId: 0 },
        courseTypeOptions: [{label:'视频',value:1},{label: '音频',value: 2}, {label: '文档',value: 3}, {label: '图片', value: 4}],
        gradeOptions: [{label: '幼儿', value: 1}, {label: '小学', value: 2}, {label: '初中', value: 3}, {label: '高中', value: 4}],
        batchArr: [],
        contentClass: [],  //内容分类可选框
        platterArr: [],
        tagArr: [],
        contentCollect: [],
        childrenOption: [],  //子账号可选框
        contentOptions: [],
        readSnVisible: false,
        noReadSnVisible: false,
        readSnData: [],
        noReadSnData: [],
        count: 1,
        classCollectData :[],
        defaultProps: {
          children: 'children',
          label: 'name'
        }
      }
    },
    created() {
      this.colData = objectable(this.checkboxVal, this.checkboxVal); //转为数组对象，好控制table
      this.getContentTableData()
      this.getBatchTwo()
      this.getPlatter()
      // this.getContentClass()
      this.getTag()
      // this.getContentCollect()
      this.getChildAdmin()
      this.getClassCollect()
    },
    methods: {
      Refresh() {
        this.listQuery = { startSize: 1, pageSize: 10, userId: localStorage.getItem("ID"), content_name: undefined, className: undefined, tagName: undefined, collectName: undefined, batch: undefined}
        this.getContentTableData()
      },

      //搜索,1:课程名称
      idnameSearch() {
        if (!this.select) return this.$message.warning("请先选择查询条件");
        this.listQuery.startSize = 1
        this.listQuery.pageSize = 10
        switch (this.select) {
          case '1':
            this.listQuery.content_name = this.searchTable
            this.getContentTableData();
            break;
          case '2':
            this.listQuery.className = this.searchTable
            this.getContentTableData();
            break;
          case '3':
            this.listQuery.tagName = this.searchTable
            this.getContentTableData();
            break;
          case '4':
            this.listQuery.collectName = this.searchTable
            this.getContentTableData();
            break;
          case '5':
            this.listQuery.batch = this.searchTable
            this.getContentTableData();
            break;
        }
      },
      //表格排序事件
      tableSortCge(column) {
        let order = '', field = '';
        column.order == "descending" ? order = 'desc' : order = 'asc'
        switch (column.prop) {
          case 'content_type':
            field = 'content_type'
            break;
          case 'contentClass':
            field = 'class_ids'
            break;
          case 'contentTag':
            field = 'tag_ids'
            break;
          case 'contentCollect':
            field = 'collect_ids'
            break;
          case 'grades':
            field = 'grades'
            break;
        }
        let sort = [{field: field, sort: order}]
        this.listQuery.sort = JSON.stringify(sort)
        this.getContentTableData();
      },
      openReadSn(id) {
        this.readSnVisible=true
        this.hasReadQuery.content_id = id
        this.getSnReadContentData()
      },
      getSnReadContentData() {
        getSnReadContent(this.hasReadQuery).then(res => {
          this.readSnData = res.data
          this.hasReadTotal = res.total
        }).catch(err => {
          this.readSnData = []
          this.hasReadTotal = 0
        })
      },
      openNoReadSn(id) {
        this.noReadSnVisible=true
        this.noReadQuery.content_id = id
        this.getNoReadContentData()
      },
      getNoReadContentData() {
        getNoReadContent(this.noReadQuery).then(res => {
          this.noReadSnData = res.data
          this.noReadTotal = res.total
        }).catch(err => {
          this.noReadSnData = []
          this.noReadTotal = 0
        })
      },
      getBatchTwo() {
        getBatchTwo({ userId: localStorage.getItem("ID") }).then(res => {
          this.batchArr = res.data
        }).catch(err => {
          this.batchArr = []
        })
      },
      getPlatter() {
        getPlatter({userId: localStorage.getItem("ID")}).then(res => {
          this.platterArr = res.data
        }).catch(err => {
          this.platterArr = []
        })
      },
      getContentClass() {
        getContentClass({userId: localStorage.getItem("ID")}).then(res => {
          this.contentClass = res.data
        }).catch(err => {
          this.contentClass = []
        })
      },
      getTag() {
        getTag({userId: localStorage.getItem("ID")}).then(res => {
          this.tagArr = res.data
        }).catch(err => {
          this.tagArr = []
        })
      },
      getContentCollect() {
        getContentCollect({userId: localStorage.getItem("ID")}).then(res => {
          this.contentCollect = res.data
        }).catch(err => {
          this.contentCollect = []
        })
      },
      getChildAdmin() {
        getChildAdmin({ userId: localStorage.getItem("ID") }).then(res => {
          this.childrenOption = res.data
        }).catch(err => {
          this.childrenOption = []
        })
      },
      getClassCollect(){
        getClassCollectTree({
          userId: localStorage.getItem("ID")
        }).then(res => {
          this.classCollectData = res.data
        }).catch(err => {
          this.classCollectData = []
        })
      },
      setContentOptions(event) {
        let param = {
          userId: localStorage.getItem('ID'),
          type: event
        }
        setContentOptions(param).then(res => {
          this.contentOptions = res.data
        }).catch(err => {
          this.contentOptions = []
        })
      },
      setAddForm(relation_id) {
        this.contentOptions.filter(item =>{
          if(item.id == relation_id) {
            this.addForm.content_name=item.name,this.addForm.relation_id=item.id,this.addForm.content_size=item.content_size,this.addForm.content_explain=item.content_explain
            this.addForm.content_url=item.content_url
            this.addForm.content_type=item.content_type
            this.addForm.suitable_grade=item.suitable_grade
            this.addForm.platterId=item.platter_id
          }
        })
      },
      openEdit(row) {
        this.formVisible = true
        this.editSubjects=true
        this.addForm = {
          content_name: row.content_name,
          content_type: row.content_type,
          content_size: row.content_size,
          content_explain: row.content_explain,
          content_url: row.content_url,
          sort: row.sort,
          batch: row.batchArr,
          classId: row.contentClassArr,
          tagId: row.contentTagArr,
          collectId: row.contentCollectArr,
          suitable_grade: row.suitable_grade,
          platterId: row.platterId,
          send_type: row.send_type,
          childAdmin: row.childAdminArr,
          content_id: row.content_id,
          upload_type: row.upload_type,
          relation_id: row.relation_id, //upload_type为1时为self_content表content_id 为2时为course表主键id
          file_list: row.file_list
        }
        //设置分类合集勾选状态
        this.$nextTick(async ()=> {
          //todo 合集的id和分类的id相同的时候会有bug 没有勾选这个id的分类也会显示为勾选 目前没想到办法解决
          this.$refs.tree.setCheckedKeys(row.contentCollectArr, true)  //只需设置合集的勾选状态,分类的会自动显示
        })
      },
      addContent(formName) {
        let allCheckedNode = this.$refs.tree.getCheckedNodes()
        let classId = allCheckedNode.map(item => item.children ? item.id : undefined).filter(id => id !== undefined);
        let collectIdArr = this.$refs.tree.getCheckedKeys(true)
        this.$refs[formName].validate(valid => {
          if (valid) {
            let addForm = JSON.parse(JSON.stringify(this.addForm))
            addForm.last_ip = sessionStorage.getItem("Ip")
            addForm.userId = localStorage.getItem("ID")
            addForm.classId = classId
            addForm.collectId = collectIdArr
            addContentControl(addForm).then(res => {
              if(res.code==200){
                this.$message.success(res.msg)
                this.getContentTableData()
              }
              this.formVisible = false
            }).catch(err => {
            })
          } else {
            return false;
          }
        });
      },
      getContentTableData() {
        queryContentControl(this.listQuery).then(res => {
          if(res.code==200){
            this.tableData = res.data
            this.total = res.total
          }else{
            this.$message.error(this.res.msg)
          }
        }).catch(err => {
          this.tableData = []
          this.total = 0
        })
      },
      editContent(formName) {
        let allCheckedNode = this.$refs.tree.getCheckedNodes()
        let classId = allCheckedNode.map(item => item.children ? item.id : undefined).filter(id => id !== undefined);
        let collectIdArr = this.$refs.tree.getCheckedKeys(true)
        this.$refs[formName].validate(valid => {
          if (valid) {
            let editForm = JSON.parse(JSON.stringify(this.addForm))
            editForm.last_ip = sessionStorage.getItem("Ip")
            editForm.userId = localStorage.getItem("ID")
            editForm.classId = classId
            editForm.collectId = collectIdArr
            editContentData(editForm).then(res => {
              this.$message.success(res.msg)
              this.formVisible = false
              this.getContentTableData()
            }).catch(err => {
            })
          } else {
            return false;
          }
        });
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;
        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
      //图片上传
      adImgUpload(res) {
        ossImage(res.raw,res.name).then(res => {//图片上传
          this.addForm.icon = res
        }).catch(err => {
          this.$message.error("图标上传失败")
        })
      },
      delContent(id) {
        this.deleteDialogs.show = true
        this.deleteDialogs.deleteId = id
      },
      //删除按钮事件
      deleteEvents() {
        let params = {
          userId: localStorage.getItem("ID"),
          last_ip: sessionStorage.getItem("Ip"),
          content_id: this.deleteDialogs.deleteId,
        }
        delContentControl(params).then(res => {
          if(res.code==200){
            this.$message.success(res.msg)
          }else{
            this.$message.error(res.msg)
          }
          this.getContentTableData()
        }).catch(err => {
          console.log("请求失败", err)
        })
      },
      //分页
      handleSizeChange(val) {
        this.listQuery.pageSize = val
        this.getContentTableData()
      },
      handleCurrentChange(val) {
        this.listQuery.startSize = val
        this.getContentTableData()
      },
      hasReadSizeChange(val) {
        this.hasReadQuery.pageSize = val
        this.getSnReadContentData()
      },
      hasReadCurrentChange(val) {
        this.hasReadQuery.startSize = val
        this.getSnReadContentData()
      },
      noReadSizeChange(val) {
        this.noReadQuery.pageSize = val
        this.getNoReadContentData()

      },
      noReadCurrentChange(val) {
        this.noReadQuery.startSize = val
        this.getNoReadContentData()

      },
      //下拉动态table子组件
      alterCheckbok(key, conte) {
        this.key = key;
        this.colData.filter(i => {
          if (conte.indexOf(i.title) != -1) {
            i.istrue = false;
            this.$nextTick(() => {
              this.$refs.tableDataRef.doLayout();
            });
          } else {
            i.istrue = true;
            this.$nextTick(() => {
              this.$refs.tableDataRef.doLayout();
            });
          }
        });
      },

      //文件上传
      contentUpload(file, fileList) {
        this.addForm.content_size=file.size
        if (file.size > 629145600) {
          this.$message.info("文件大小限制为600M以内")
          return
        }
        ossFile(file.raw,file.name).then(ossUrl => {//文件上传
          this.addForm.content_url=ossUrl
          this.addForm.file_list.push({name:file.name, url: ossUrl})
          this.$message.success("上传成功")
        }).catch(err => {
          this.$message.error("文件上传失败")
        })
      },
      handleRemove(file, fileList) {
        this.addForm.file_list = this.addForm.file_list.filter(item => {
          return item.name != file.name
        })
      },
      handleExceed(files, fileList) {
        this.$message.warning('仅支持上传单文件');
      },
      //编辑添加表单关闭事件
      addFormclose() {
        this.editSubjects=false
        this.$nextTick(() => {
          this.addForm = this.$options.data().addForm
          this.$refs.tree.setCheckedKeys([]) //清空选中状态
        })
      },
    }
  }
</script>
<style scoped>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 80px;
    height: 80px;
    line-height: 80px;
    text-align: center;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
  }
  .avatar {
    width: 80px;
    height: 80px;
    display: block;
  }
</style>


